package leetcode;

import java.util.Scanner;

public class IntegerAdd {

	public static void main(String[] args) {
//		Scanner scanner = new Scanner(source)
	}
	public static void main(String s1, String s2){
		boolean s1Tag = false;
		boolean s2Tag = true;
		if(s1.charAt(0) == '+'){
			s1 = s1.substring(1, s1.length());
		}
		if(s1.charAt(0) == '-'){
			s1 = s1.substring(1, s1.length());
			s1Tag = true;
		}
		if(s2.charAt(0) == '+'){
			s2 = s2.substring(1, s2.length());
		}
		if(s2.charAt(0) == '-'){
			s2 = s2.substring(1, s2.length());
			s2Tag = true;
		}
		char[] snumber1 = s1.toCharArray();
		char[] snumber2 = s2.toCharArray();
		int[] num1 = new int[snumber1.length];
		int[] num2 = new int[snumber2.length];
		// 注意，char数组中最低位存放着最高位
		for (int i = 0; i < snumber1.length; i++) {
			num1[i] = snumber1[snumber1.length - 1 - i] - '0';
		}
		for (int i = 0; i < snumber2.length; i++) {
			num2[i] = snumber2[snumber2.length - 1 - i] - '0';
		}
		int len = Math.min(snumber1.length, snumber2.length);
	    int maxlen = Math.max(snumber1.length, snumber2.length);
		int[] sum = new int[maxlen];
		int carry = 0; //进位
		if(! (s1Tag ^ s2Tag)){
			for (int i = 0; i < len; i++) {
				carry = num1[i] + num2[i] + carry;
				sum[i] = carry % 10;
				carry = carry / 10;
			}
		}
		//用于指示哪个长
		final int indicator = snumber1.length > len ? 0 : -1;
		for (int i = len; i < maxlen; i++) {
			if (indicator == 0) {
				carry = num1[i] + carry;
			} else {
				carry = num2[i] + carry;
			}
			sum[i] = carry % 10;
			carry = carry / 10;
		}
		if(carry == 1){
            char[] result = new char[maxlen + 1];
            for(int i = maxlen; i > 0; i--){
                result[maxlen + 1 - i] = (char) (sum[i - 1] + '0');
            }
            result[0] = '1';
            System.out.println(String.valueOf(result));
        }else{
            char[] result = new char[maxlen];
            for(int i = maxlen - 1; i >= 0; i--){
                result[maxlen - 1 - i] = (char) (sum[i - 1] + '0');
            }
            System.out.println(String.valueOf(result));
        }
	}
}
